package com.glzn.springboot03.controller;

import com.github.pagehelper.PageInfo;
import com.glzn.springboot03.entity.Event;
import com.glzn.springboot03.service.EventService;
import com.glzn.springboot03.util.VeDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author gongjq
 */
@Controller
public class  EventController {
	private static final Logger logger = LoggerFactory.getLogger(EventController.class);

	private static final String ALL_TYPE = "所有类型";

	@Autowired
	private EventService eventService;

	private List<String> eventType = new ArrayList<>();

	@RequestMapping("/events")
	public String event(ModelMap modelMap,
	                    HttpSession session,
	                    @RequestParam(required = false) String eventType,
	                    @RequestParam(required = false) String startTime,
	                    @RequestParam(required = false) String endTime,
	                    @RequestParam(required = false, defaultValue = "1") int pageNum,
	                    @RequestParam(required = false, defaultValue = "10") int pageSize){
		Integer deviceId = (Integer) session.getAttribute("device_id");
		//1、一开始，所以的参数都是为空的，需要创建类型和起始结束时间
		if (eventType==null || "".equals(eventType)){
			eventType = ALL_TYPE;
		}
		if (startTime==null || endTime==null || "".equals(startTime) || "".equals(endTime)){
			//获取今天的日期
			Date today = new Date();
			endTime = VeDate.dateToStr(today);
			Calendar c = Calendar.getInstance();
			c.setTime(today);
			c.add(Calendar.MONTH, -1);
			//这是昨天
			Date yesterday = c.getTime();
			startTime = VeDate.dateToStr(yesterday);
		}
		//2、根据事件类型和起始结束事件查询事件
		List<Event> eventList = eventService.findByTimeAndType(VeDate.strToDate(startTime), VeDate.strToDate(endTime), eventType, deviceId);
		if (ALL_TYPE.equals(eventType)){
			eventList = eventService.findByTime(VeDate.strToDate(startTime), VeDate.strToDate(endTime), deviceId);
		}
		this.eventType.clear();
		this.eventType.add(ALL_TYPE);
		for (Event event : eventList) {
			int i = 0;
			for (i = 0; i < this.eventType.size(); i++) {
				if (this.eventType.get(i).equals(event.getType())){
					break;
				}
			}
			if (i == this.eventType.size()){
					this.eventType.add(event.getType());
			}
		}
		//3、查询网页要显示的事件
		PageInfo<Event> pageInfo = eventService.findByTimeAndType(VeDate.strToDate(startTime), VeDate.strToDate(endTime), eventType, pageNum, pageSize, deviceId);
		if (ALL_TYPE.equals(eventType)){
			pageInfo = eventService.findByTime(VeDate.strToDate(startTime), VeDate.strToDate(endTime), pageNum, pageSize, deviceId);
		}
		long totalNum = pageInfo.getTotal() / pageSize + 1;

		modelMap.put("eventType", this.eventType);
		modelMap.put("startTime", startTime);
		modelMap.put("endTime", endTime);
		modelMap.put("pageInfo", pageInfo);
		modelMap.put("totalNum", totalNum);
		return "events";
	}

	@RequestMapping("/getevent")
	@ResponseBody
	public String getEvent() throws UnsupportedEncodingException {
		return eventService.listEvent();
	}

	@GetMapping(value = "/getevent/getprocess")
	@ResponseBody
	public int getProcess(HttpSession session){
		return eventService.getProcess(session.getId());
	}

	@GetMapping("/wx/event")
	@ResponseBody
	public List<Event> wxEvent(HttpSession session, String startTime, String endTime, String type){
		Integer deviceId = (Integer) session.getAttribute("device_id");
		if (type == null || "".equals(type) || ALL_TYPE.equals(type)){
 			return eventService.findByTime(VeDate.strToDate(startTime), VeDate.strToDate(endTime), deviceId);
		}else {
			return eventService.findByTimeAndType(VeDate.strToDate(startTime), VeDate.strToDate(endTime), type, deviceId);
		}
	}
}
